package com.eric.nioclient.service;

import android.os.Handler;

import com.eric.nioclient.service.packet.ChatToGroupPacket;
import com.eric.nioclient.service.packet.UserRegisterPacket;

import java.util.ArrayList;
import java.util.List;

import io.netty.channel.Channel;

/**
 * Created by eric on 2017/11/17.
 */

public class Logic {
    private static Logic logic ;
    public static Logic me(){
        if(logic == null){
            logic = new Logic();
        }
        return logic;
    }

    private String user= "eric",room;

    private Handler mH = new Handler();
    private Channel channel ;
    public void setChannel(Channel channel){
        this.channel = channel;
    }

    private List<ServiceListener> listeners = new ArrayList<>();
    public void addListener(ServiceListener listener){
        this.listeners.add(listener);
    }
    public void removeListener(ServiceListener listener){
        this.listeners.remove(listener);
    }
    public void noticeServiceStarted(){
        for (ServiceListener listener : listeners){
            listener.started();
        }
    }
    public void fireServiceStoped(){
        mH.post(new Runnable() {
            @Override
            public void run() {
                for (ServiceListener listener : listeners){
                    listener.stoped("服务已经停止.");
                }
            }
        });

    }
    public void fireServiceStarted(){
        mH.post(new Runnable() {
            @Override
            public void run() {
                for (ServiceListener listener : listeners){
                    listener.started();
                }
            }
        });

    }
    public void fireServiceNotice(final String msg){
        mH.post(new Runnable() {
            @Override
            public void run() {
                for (ServiceListener listener : listeners){
                    listener.receivedNotice(msg);
                }
            }
        });

    }
    public void fireReceivedMsg(final String user,final String msg){
        mH.post(new Runnable() {
            @Override
            public void run() {
                for (ServiceListener listener : listeners){
                    listener.receivedMsg(user,msg);
                }
            }
        });

    }

    public void fireLoginStatus(final boolean isSuccess){
        mH.post(new Runnable() {
            @Override
            public void run() {
                for (ServiceListener listener : listeners){
                    if(isSuccess){
                        listener.loginSuccess();
                    }else{
                        listener.loginFail("登录失败");
                    }
                }
            }
        });

    }

    public void fireConnectActive(){
        mH.post(new Runnable() {
            @Override
            public void run() {
                for (ServiceListener listener : listeners){
                    listener.connectActive();
                }
            }
        });

    }

    public boolean isMe(String user){
        return this.user.equals(user);
    }


    public void login(String user,String room){
        this.user = user;
        this.room = room;
        channel.writeAndFlush(new UserRegisterPacket(user,room));
    }

    public void sendMsg(String msg){
        channel.writeAndFlush(ChatToGroupPacket.obtain(this.user,this.room,msg));
    }

    /**
     * 启动服务的监听
     */
    public interface ServiceListener {
        /**
         * 服务已经启动
         */
        void started();

        /**
         * 服务停止
         * @param obj
         */
        void stoped(Object obj);

        /**
         * 链接激活
         */
        void connectActive();

        /**
         * 接受到消息
         * @param sendUser
         * @param msg
         */
        void receivedMsg(String sendUser,String msg);

        /**
         * 接受到系统通知
         * @param msg
         */
        void receivedNotice(String msg);

        /**
         * 登录成功
         */
        void loginSuccess();
        /**
         * 登录失败
         */
        void loginFail(String msg);

    }

}
